package com.zx.tradeclient.ctrl;

/*
 * 区域/类别
 * 
 * */

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.tradeclient.R;
import com.zx.tradeclient.adapter.CategoryAdapter;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.entity.CategoryInfo;

public class CategoryActivity extends BaseActivity implements OnClickListener {

	private ListView list;
	private CategoryAdapter adapter;

	private TradeApp app;

	private int categ = 0; // 0是分类 1是地区

	private ProgressDialog progressDialog;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:

				progressDialog = ProgressDialog.show(CategoryActivity.this, null, "正在获取数据，请稍后.....", true, true);

				if (0 == categ) {
					app.mCateg.clear();

				} else if (1 == categ) {
					app.mArea.clear();

				}

				getProvinceInfo();

				break;
			case 1:

				progressDialog.dismiss();
				adapter.notifyDataSetChanged();

				break;
			case 2:
				progressDialog.dismiss();

				Toast toast2 = Toast.makeText(CategoryActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

				break;

			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category_activity);

		if (!(ActivityTaskManager.getInstance().containsName("CategoryActivity"))) {
			ActivityTaskManager.getInstance().putActivity("CategoryActivity", this);
		}

		init();
	}

	private void init() {

		app = (TradeApp) getApplication();

		categ = Integer.parseInt(getIntent().getStringExtra("categ"));

		setHeader(0);

		header_right.setVisibility(View.VISIBLE);
		header_right = (Button) findViewById(R.id.header_right);
		header_right.setBackgroundResource(R.drawable.header_dnone);
		header_right.setText(R.string.header_right_area);
		header_right.setOnClickListener(this);

		if (0 == categ) {
			header_right.setText(R.string.header_right_area);

			if (0 == app.mCateg.size()) {
				Message msg0 = new Message();
				msg0.what = 0;
				handler.sendMessage(msg0);
			}

			adapter = new CategoryAdapter(this, app.mCateg);
		} else if (1 == categ) {
			header_right.setText(R.string.header_right_category);
			if (0 == app.mArea.size()) {
				Message msg0 = new Message();
				msg0.what = 0;
				handler.sendMessage(msg0);
			}

			adapter = new CategoryAdapter(this, app.mArea);
		}

		list = (ListView) findViewById(R.id.category_list);
		list.setAdapter(adapter);
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
				Intent intent = new Intent(CategoryActivity.this, SecondCategoryActivity.class);
				if (0 == categ) {
					intent.putExtra("provinceId", app.mCateg.get(position).getCategory_id());
				} else if (1 == categ) {
					intent.putExtra("provinceId", app.mArea.get(position).getCategory_id());
				}

				intent.putExtra("categ", Integer.toString(categ));
				startActivity(intent);
			}
		});

	}

	private void getProvinceInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		if (0 == categ) {
			map.put("act", "productTypeList");
		} else if (1 == categ) {
			map.put("act", "provinceCity");
		}

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				Message msg2 = new Message();
				msg2.what = 2;
				handler.sendMessage(msg2);

			}
		});
	}

	private void parseJson(JSONObject response) {
		try {
			String succeed = response.getString("succeed");
			if (succeed.equals("000")) {
				String data = response.getString("dataList");

				JSONArray list = new JSONArray(data);

				for (int i = 0; i < list.length(); i++) {
					CategoryInfo info = new CategoryInfo();
					JSONObject log = list.getJSONObject(i);
					String cat_id = log.getString("id");
					String cat_name = log.getString("name");

					info.setCategory_id(cat_id);
					info.setCategory_name(cat_name);

					if (0 == categ) {
						app.mCateg.add(info);
					} else if (1 == categ) {
						app.mArea.add(info);
					}

				}
				Message msg1 = new Message();
				msg1.what = 1;
				handler.sendMessage(msg1);

			} else {
				Message msg2 = new Message();
				msg2.what = 2;
				handler.sendMessage(msg2);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {

			goback();

		} else if (v == header_right) {
			if (getString(R.string.header_right_category).equals(header_right.getText().toString())) {
				header_right.setText(R.string.header_right_area);

				categ = 0;
				adapter = new CategoryAdapter(CategoryActivity.this, app.mCateg);
				list.setAdapter(adapter);

				if (app.mCateg.size() == 0) {
					Message msg0 = new Message();
					msg0.what = 0;
					handler.sendMessage(msg0);
				} else {
					adapter.notifyDataSetChanged();
				}

			} else if (getString(R.string.header_right_area).equals(header_right.getText().toString())) {
				header_right.setText(R.string.header_right_category);
				categ = 1;
				adapter = new CategoryAdapter(CategoryActivity.this, app.mArea);
				list.setAdapter(adapter);

				if (app.mArea.size() == 0) {
					Message msg0 = new Message();
					msg0.what = 0;
					handler.sendMessage(msg0);
				} else {
					adapter.notifyDataSetChanged();
				}
			}
		}
	}

}
